<template>
    <div class="my-confirm" :class="{fade: fade}">
        <div class="wrapper" :class="{fade: fade}">
            <div class="header" v-if="showHeader">
                <span
                    class="iconfont icon-tip"
                    v-if="iconShow"
                    :class="[style[type]?.icon]"
                    :style="style[type]"
                >
                </span>
                <h3 :class="{pl24: !iconShow}" style="fontweight: bold">
                    {{ title }}
                </h3>
                <a
                    @click="cancelCallback"
                    href="JavaScript:;"
                    v-if="showClose"
                    class="iconfont icon-a-guanbidanchuang iconClose"
                ></a>
            </div>
            <div class="body" :class="{pt20: !showHeader}">
                <span
                    class="iconfont icon-tip"
                    v-if="!showHeader && iconShow"
                    :class="tipType"
                >
                </span>
                <textItem :text="text"></textItem>
            </div>
            <div class="footer" v-if="showFooter">
                <el-button
                    v-if="showCancelButton"
                    @click="cancelCallback"
                    class="srt-el-button srt-el-button--cancel srt-el-button-h36"
                    >{{ cancelText }}</el-button
                >
                <el-button
                    :loading="confirmLoading?.value"
                    @click="submitCallback"
                    class="srt-el-button srt-el-button--primary srt-el-button-h36 ml20"
                    >{{ submitText }}</el-button
                >
            </div>
        </div>
    </div>
</template>
<script setup>
import {onMounted, ref, computed, defineComponent} from 'vue';
import {ElButton} from 'element-plus';

const textItem = defineComponent({
    props: {text: undefined},
    setup(props) {
        if (typeof props.text === 'function') return props.text;
        return () => <span>{props.text}</span>;
    }
});

const props = defineProps({
    title: {
        type: String,
        default: ''
    },
    text: {
        type: [String, Function],
        default: ''
    },
    cancelCallback: {
        type: Function,
        required: true
    },
    submitCallback: {
        type: Function,
        required: true
    },
    cancelText: {
        type: String,
        default: '取消'
    },
    submitText: {
        type: String,
        default: '确认'
    },
    type: {
        type: String,
        default: 'info'
    },
    iconShow: {
        type: Boolean,
        default: true
    },
    showClose: {
        type: Boolean,
        default: true
    },
    showCancelButton: {
        type: Boolean,
        default: true
    },
    showHeader: {
        type: Boolean,
        default: true
    },
    confirmLoading: {
        type: Object
    },
    showFooter: {
        type: Boolean,
        default: true
    }
});
const fade = ref(false);
onMounted(() => {
    setTimeout(() => {
        fade.value = true;
    }, 0);
});
const tipTypeData = {
    hint: 'icon-tishi',
    success: 'icon-dui1'
};
const tipType = computed(() => {
    const tip = props.type;

    return tipTypeData[tip];
});
const style = {
    info: {
        icon: 'icon-tishi',
        color: '#FF9201'
    },
    hint: {
        icon: 'icon-tishi',
        color: '#FF9201'
    },
    error: {
        icon: 'icon-guanbi1',
        color: '#FF4719'
    },
    success: {
        icon: 'icon-dui1',
        color: '#00B042'
    }
};
</script>
<style scoped lang="scss">
.my-confirm {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 88888;
    background: rgba(0, 0, 0, 0);
    &.fade {
        transition: all 0.4s;
        background: rgba(0, 0, 0, 0.5);
    }
    .wrapper {
        width: 480px;
        background: #fff;
        border-radius: 4px;
        position: absolute;
        box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.06);
        top: 50%;
        left: 50%;
        transform: translate(-50%, -60%);
        opacity: 0;
        &.fade {
            transition: all 0.4s;
            transform: translate(-50%, -50%);
            opacity: 1;
        }

        .footer {
            height: 36px;
            line-height: 36px;
            padding: 0 20px;
        }
        .body {
            padding: 0px 20px 40px 50px;
            font-size: 16px;
            color: #000;
            display: flex;
            flex-wrap: wrap;
            word-break: break-all;
            line-height: 1.5;
        }
        .footer {
            text-align: right;
            margin-bottom: 20px;
        }
        .header {
            position: relative;
            display: flex;
            padding: 20px;
            padding-bottom: 16px;
            align-items: center;
            h3 {
                font-size: 16px;
                color: #000000;
            }
        }
    }
}
.ml20 {
    margin-left: 20px;
}
.pt20 {
    padding-top: 20px !important;
}
.pl24 {
    padding-left: 24px;
}
.icon-tip {
    font-size: 24px;

    margin-right: 6px;
}
.icon-tishi {
    color: #ff9201;
}
.icon-dui1 {
    color: #00b042;
}
.iconClose {
    position: absolute;
    right: 20px;
    top: 20px;
    font-size: 14px;
    color: #000;

    width: 20px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    text-decoration: none;
    color: #000;
    &:hover {
        color: #666;
    }
}
</style>
